package com.sky.controller.admin;


import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishFlavorService;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品管理")
public class DishController {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;

    @GetMapping("/page")
    @ApiOperation("菜品分页")
    public Result<PageResult> pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    @PostMapping
    @ApiOperation("新增菜品")
    public Result addDish(@RequestBody DishDTO dishDTO) {
        dishService.addDish(dishDTO);
        String key = "dish_" + dishDTO.getCategoryId();
        CleanCache(key);
        return Result.success();
    }


    @GetMapping("{id}")
    @ApiOperation("修改菜品页面回显")
    public Result<DishVO> toRevisePage(@PathVariable Long id) {
        Dish dish = dishService.getDishById(id);
        DishVO dishVO = new DishVO();//返回给前端
        BeanUtils.copyProperties(dish, dishVO);
        //关联id的所有口味
        List<DishFlavor> flavors = dishFlavorService.getDishFlavorById(id);
        dishVO.setFlavors(flavors);
        return Result.success(dishVO);
    }

    @PutMapping
    @ApiOperation("修改菜品")
    public Result reviseDishInfo(@RequestBody DishDTO dishDTO) {
        //对dish进行更新
        dishService.reviseDishInfo(dishDTO);
        //对flavor表进行更新
        dishFlavorService.updateFlavor(dishDTO);
        CleanCache("dish_*");
        return Result.success();
    }


    @PostMapping("/status/{status}")
    @ApiOperation("修改状态")
    public Result stopAndStart(@PathVariable Integer status, Integer id) {
        dishService.stopAndStart(status, id);
        CleanCache("dish_*");
        return Result.success();
    }

    @DeleteMapping
    @ApiOperation("删除")
    public Result deleteDish(@RequestParam List<Long> ids) {
        dishService.deleteDish(ids);
        dishFlavorService.deleteDishFlavor(ids);
        CleanCache("dish_*");
        return Result.success();
    }

    @GetMapping("/list")
    @ApiOperation("根据分类查询dish")
    public Result<List<Dish>> getDishList(Long categoryId) {
        List<Dish> dishList = dishService.getDishList(categoryId);
        return Result.success(dishList);
    }

    //清理缓存
    private void CleanCache(String pattern) {
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
